package irio.gamer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JList;

import Logic.GamerParams;
import Logic.Movie;

public class Preview {

	GamerImage gamerImage;
	int currentSize = 32;
	
	public boolean postProcessing = false;
	
	public Preview(GamerImage gi) {
		gamerImage = gi;
		
	}
    public void resetPreview(boolean postp) {
    	currentSize=32;
    
    	postProcessing = postp;

    	if (postProcessing)
    		gamer.executeGamer(false);
    	
    	if (p!=null)
    		p.destroy();
    	
    
    	
    }

	
	public void Update() {
    	new Thread(new Runnable() {
 		   public void run() {
 			   while (1==1) {

 				   try {
 					   if (!SettingsPanel.verifyMpiRun() || GamerParams.isRendering || gamer.gamerParams.preview.x==0 || Movie.isFilming == true) {
 						   Thread.sleep(1000);
 						   gamer.btnRun.setEnabled(false);
 					   }
 				   else {
 					   if (gamer.btnRun != null)
						   gamer.btnRun.setEnabled(true);
					   //currentSize = 128;
 					   if (currentSize != 256) {
 					   //if (!postProcessing)
 					   executePreview();
 					   createPreviewImage();
 					   currentSize*=2;
 				   }
 				   }
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
 			   }
	    	}
 		   }).start();			

	}
	
	   Process p = null;
		
    public void executePreview() {
    	if (gamer.galaxy==null)
    		return;
				try {
					double rs = gamer.rp.raystep.x;
					gamer.rp.raystep.x = 0.1;
					gamer.rp.raystep.x = rs;
					gamer.createScene();
					gamer.rp.Save("renderparameters.ini");
			    	gamer.galaxy.Save(gamer.galaxy.filename);

					p = Runtime.getRuntime().exec(gamer.createGamerCommand(currentSize, gamer.gamerParams.wavelengths.z, "output/preview.fits"));
					p.waitFor();
					gamer.rp.Save("renderparameters.ini");

				
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    		   }

    public void createPreviewImage() {
		try {
			gamer.Convolve("output/preview.fits", "output/preview2.fits");
/*			Process p2 = Runtime.getRuntime().exec("bin/gutil create_rgb  output/preview2.fits output/preview2.fits output/preview2.fits " + gamerImage.filename + " " +
						 gamer.gamerParams.color.x + " " + gamer.gamerParams.color.y + " " +gamer.gamerParams.color.z + " " +gamer.gamerParams.power.x +  " " + gamer.gamerParams.cutoff.x);;
*/
			
			Process p2 = Runtime.getRuntime().exec("bin/gutil create_rgb  output/preview2.fits output/preview2.fits output/preview2.fits " + gamerImage.filename + " " +
						1.0 + " " + 1.0 + " " +1.0 + " " +gamer.gamerParams.power.x +  " " + gamer.gamerParams.cutoff.x);;
					p2.waitFor();
				gamerImage.loadImage();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    }


}
